package com.example.demo.service.dataSource1.impl;

import com.example.demo.dao.dataSource1.MemberMapper;
import com.example.demo.entity.bean.dataSource1.MemberBean;
import com.example.demo.service.dataSource2.ESDictService;
import com.example.demo.util.StringDigest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Wen Gao on 2018/7/11.
 */
@Service
@Transactional(transactionManager = "xatx", propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public class MemberService {

    private static final Logger logger = LogManager.getLogger(MemberService.class);

    public static final String[] ORDER_BY = {
            " member_uid ASC",
            " member_uid DESC",
            " name ASC",
            " name DESC",
            " email ASC",
            " email DESC"
    };
    public static final String[] STATUSES = {"normal", "inactivated", "deleted", "unautherized"};
    public static final String[] ROLES = {"journalist", "client"};
    public static final int ORDERBY_UID_ASC = 0;
    public static final int ORDERBY_UID_DESC = 1;
    public static final int ORDERBY_NAME_ASC = 2;
    public static final int ORDERBY_NAME_DESC = 3;
    public static final int ORDERBY_EMAIL_ASC = 4;
    public static final int ORDERBY_EMAIL_DESC = 5;

    public static final int ROLE_JOURNALIST = 0;
    public static final int ROLE_CLIENT = 1;

    public static final int STATUS_NORMAL = 0;
    public static final int STATUS_INACTIVATED = 1;
    public static final int STATUS_DELETED = 2;
    public static final int STATUS_UNAUTH = 3;


    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private ESDictService esDictService;

    public MemberBean getMemberByMemberUid(long memberUid) {
        return MemberBean.adapt(memberMapper.select(memberUid));
    }

    public void addMember(MemberBean memberBean) {
        memberMapper.insert(memberBean);
    }

    public void updateMember(MemberBean memberBean) {
        memberMapper.update(memberBean);
    }

    /**
     * 根据name和email的过滤条件, 页面起始和页面大小, 获取member列表
     *
     * @param start 起始index
     * @param limit 查多少条
     * @param orderby 排序字段
     * @param name_key name
     * @param email_key email
     * @return
     */
    public List<MemberBean> getMemberBeanByPageAndCondition(int start,
                                                        int limit,
                                                        int orderby,
                                                        String name_key,
                                                        String email_key,
                                                        int role,
                                                        boolean with_profiles,
                                                        int bureau) {
        int[] roles = null;
        if (role >= 0 && role < ROLES.length)
            roles = StringDigest.indexToPossibleDecimals(role, ROLES.length);

        Map<String, Object> parameters = new HashMap<String, Object>();

        if (start < 0) {
            parameters.put("start", 0);
        } else {
            parameters.put("start", start);
        }
        if (limit <= 0) {
            parameters.put("limit", 10);
        } else {
            parameters.put("limit", limit);
        }
        if (orderby > ORDER_BY.length - 1 || orderby < 0) {
            parameters.put("orderby", ORDER_BY[0]);
        } else {
            parameters.put("orderby", ORDER_BY[orderby]);
        }
        if (name_key != null && name_key.length() > 0) {
            parameters.put("name_key", '%' + name_key + '%');
        }
        if (email_key != null && email_key.length() > 0) {
            parameters.put("email_key", '%' + email_key + '%');
        }
        if (roles != null && roles.length > 0) {
            parameters.put("roles", roles);
        }
        if (with_profiles) {
            parameters.put("with_profiles", true);
        }
        if (bureau > -1) {
            parameters.put("bureau", bureau);
        }

        List<Long> uids = memberMapper.selectByPageAndCondition(parameters);
        return adaptIdList(uids);

    }

    public List<MemberBean> adaptIdList(List<Long> uids) {
        List<MemberBean> memberBeanList = new ArrayList<>();
        for (Long uid : uids) {
            memberBeanList.add(getMemberByMemberUid(uid));
        }
        return memberBeanList;
    }

}
